﻿using System;
using System.Collections;
using System.Collections.Generic;
using IDAL;
using Model;

namespace BLL
{
    public class FavoriteService
    {
        private static readonly IFavouriteDAL dal = DALFactory.DataAccess.createFavouriteDAL();

        //添加收藏
        public Hashtable addFavourite(User user, int resourceId)
        {
            Hashtable ht = new Hashtable();
            Resource resource = new ResourceService().getResourceById(resourceId);
            FavoriteService fs=new FavoriteService();
            IList<Favourite> fl=fs.getResourceFavourite(resourceId);
            int haveF=0;
            int fint = 0;
            foreach(Favourite mf in fl)
            {
                if(mf.user.id==user.id){
                    haveF=1;
                    fint = mf.id;
                }
            }
            if (resource == null)
            {

                ht.Add("result", "0");
                ht.Add("message", "资源不存在");
                return ht;
            }
            else if (haveF==1)
            {
                ht.Add("result", "0");
                ht.Add("message", "取消收藏！");
                fs.delFavourite(fint);
                return ht;
            }
            Favourite favourite = new Favourite()
            {
                user = user,
                resource = resource,
                time = DateTime.Now
            };
            try
            {
                dal.saveFavorite(favourite);
            }
            catch (Exception e)
            {
                ht.Add("result", "0");
                ht.Add("message", e.Message);
                return ht;
            }
            ht.Add("result", "1");
            ht.Add("message", "收藏成功");
            return ht;
        }

        public Favourite getFavourite(User user, Resource resource)
        {
            Favourite f=dal.getFavourite(user,resource);
            return f;
        }

        //删除收藏
        public Hashtable delFavourite(int favouriteId)
        {
            Hashtable ht = new Hashtable();
            Favourite favourite = dal.getFavourite(favouriteId);
            if (favourite == null)
            {
                ht.Add("result", "0");
                ht.Add("message", "资源不存在");
                return ht;
            }
            try
            {
                dal.delFavorite(favourite);
            }
            catch (Exception e)
            {
                ht.Add("result", "0");
                ht.Add("message", e.Message);
                return ht;
            }
            ht.Add("result", "1");
            ht.Add("message", "删除成功");
            return ht;
        }

        //user收藏列表
        public IList<Favourite> getUserFavourite(int userId)
        {
            return dal.getFavourite(new UserService().getUserById(userId));
        }

        //resource收藏列表
        public IList<Favourite> getResourceFavourite(int resourceId)
        {
            return dal.getFavourite(new ResourceService().getResourceById(resourceId));
        }

    }
}
